//
//  ECUIButton_Category.m
//  Edu_iOS
//
//  Created by yanyan on 2017/9/11.
//  Copyright © 2017年 yanyan. All rights reserved.
//

#import "ECUIButton_Category.h"
@interface ECUIButton_Category()
@property(nonatomic,assign)CGFloat hPadding;//间距
@end
@implementation ECUIButton_Category
-(instancetype)initWithNormalImage:(UIImage *)normalImage selectedImage:(UIImage *)selectedImage
{
    if (self = [super init]) {
    [self setTitleColor:ECTHEMESELECTEDCOLOR forState:UIControlStateSelected];
    [self setTitleColor:[UIColor colorWithRed:0.2706 green:0.2706 blue:0.2706 alpha:1] forState:UIControlStateNormal];
        _selectedState = KHButtonStateNormal;
    [self setImage:normalImage forState:UIControlStateNormal];
    [self setImage:selectedImage forState:UIControlStateSelected];
    [self addTarget:self action:@selector(action_click:) forControlEvents:UIControlEventTouchUpInside];
 }
    self.hPadding = 3;

    return self;
}
- (void)action_click:(id)sender{
    
//        if (self.selectedState == KHButtonStateSortUp) {
//            self.selected = YES;
//            // 展开状态-> 隐藏分类，选中展示
//            self.selectedState = KHButtonStateSortDown;
//            [self setImage:[UIImage imageNamed:@"category_sort_select_down"] forState:UIControlStateSelected];
//        } else if (self.selectedState == KHButtonStateSortDown){
//            // 隐藏选中状态->展开分类
//            self.selectedState = KHButtonStateNormal;
//            self.selected = NO;
//
//        } else {
//            self.selected = YES;
//            self.selectedState = KHButtonStateSortUp;
//            [self setImage:[UIImage imageNamed:@"category_sort_select_up"] forState:UIControlStateSelected];
//
//        }
    if (self.selectedState == KHButtonStateSortUp) {
//        self.selected = YES;
        // 展开状态-> 隐藏分类，选中展示
        self.selectedState = KHButtonStateSortDown;
//        [self setImage:[UIImage imageNamed:@"category_sort_select_down"] forState:UIControlStateSelected];
    } else if(self.selectedState == KHButtonStateSortDown){
        // 隐藏选中状态->展开分类
//        [self setImage:[UIImage imageNamed:@"category_sort_select_up"] forState:UIControlStateSelected];
        self.selectedState = KHButtonStateSortUp;
//        self.selected = YES;
        
    } else {
//        self.selected = YES;
        self.selectedState = KHButtonStateSortUp;
//        [self setImage:[UIImage imageNamed:@"category_sort_select_up"] forState:UIControlStateSelected];
        
    }
    if (self.block_selected) {
        self.block_selected(sender);
    }

    
}
 - (void)setSelectedState:(KHButtonState)selectedState
{
    _selectedState = selectedState;
    if (_selectedState == KHButtonStateSortUp) {
        self.selected = YES;
        [self setImage:[UIImage imageNamed:@"category_sort_select_up"] forState:UIControlStateSelected];
    } else if(_selectedState == KHButtonStateSortDown){
        // 隐藏选中状态->展开分类
        [self setImage:[UIImage imageNamed:@"category_sort_select_down"] forState:UIControlStateSelected];
        self.selected = YES;
    } else {
        self.selected = YES;
        _selectedState = KHButtonStateNormal;
        [self setImage:[UIImage imageNamed:@"category_sort_select_up"] forState:UIControlStateSelected];
        
    }
}
+(instancetype)buttonWithType:(UIButtonType)buttonType
{
    ECUIButton_Category *btn = (ECUIButton_Category *)[UIButton buttonWithType:UIButtonTypeCustom];
    return btn;
}
- (void)settingsForButton:(ECUIButton_Category*)btn{
    btn.hPadding = 3;
    btn.titleLabel.font = [UIFont systemFontOfSize:14 * kPercentage];
    btn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    btn.titleLabel.textAlignment = NSTextAlignmentCenter;
    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
}
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self settingsForButton:self];
    }
    return self;
}
//-(CGRect)titleRectForContentRect:(CGRect)contentRect
//{
////  CGSize size = [[self titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
//    CGFloat leftPPadding = (CGRectGetWidth(self.bounds)/2 - _hPadding - 13 * kPercentage)/2.0;
//        return CGRectMake(leftPPadding, 0, CGRectGetWidth(self.bounds)/2, self.bounds.size.height);
//   
//}
//-(CGRect)imageRectForContentRect:(CGRect)contentRect
//{
////    CGSize size = [[self titleForState:UIControlStateNormal] sizeWithAttributes:@{NSFontAttributeName: self.titleLabel.font}];
//    CGFloat leftPPadding = (CGRectGetWidth(self.bounds)/2
//                            - _hPadding - 13 * kPercentage)/2.0;
//    CGFloat frameY = (self.bounds.size.height - 7 * kPercentage)/2.0;
//    
//        return CGRectMake(CGRectGetWidth(self.bounds)-leftPPadding - 13 * kPercentage, frameY, 13 * kPercentage, 7 * kPercentage);
//   
//}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
